package com.winit.ogg.web;

import com.winit.common.base.bean.LoginUser;
import com.winit.common.base.web.BaseController;
import com.winit.common.util.Constants;
import com.winit.common.util.JSONFileUtil;
import com.winit.common.util.UserInfoUtil;
import com.winit.ogg.bean.Authorize;
import com.winit.ogg.manager.OggManager;
import com.winit.ogg.manager.PTServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "ogg", tags = {"ogg"}, description = "ogg")
@RequestMapping(value = "/api/v1/ogg")
public class OggController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(OggController.class);

    @Autowired
    private OggManager oggManager;

    @Value("${beetle_auth_url}")
    private String BEETLE_AUTH_URL;

    @Autowired
    private PTServiceImpl operateService;


    @RequestMapping(value = "/do", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "操作授权/取消授权/启动同步/停止同步")
    public
    @ResponseBody
    Map toDo(@ApiParam(value = "登陆tokenId", required = true) @RequestParam(value = "tokenId", required = true) String tokenId,
             @ApiParam(value = "操作类型,auth_qa/auth_ops/cancel_qa/cancel_ops/cancel_all/start_ogg/stop_ogg", required = true) @RequestParam(value = "type", required = true) String type,
             HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.doAuthorize: tokenId=" + tokenId + ",type=" + type);
        /*
         * 1.校验用户是否存在
         * 2.校验用户是否拥有角色权限
         */
        LoginUser userInfo = UserInfoUtil.getUserInfo(tokenId, BEETLE_AUTH_URL);
        if (userInfo == null) {
            return this.noUserRtn();
        }
        return oggManager.toDo(userInfo, type);
    }

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询状态")
    public
    @ResponseBody
    Authorize getStatus(HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.getStatus");
        return oggManager.getStatus();
    }

    @RequestMapping(value = "/ogg", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询ogg服务状态")
    public
    @ResponseBody
    Map getOggStatus(HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.getOggStatus");
        return operateService.status();
    }

//    @RequestMapping(value = "/total", method = RequestMethod.GET)
//    @ResponseStatus(HttpStatus.OK)
//    @ApiOperation(value = "查询同步数据量")
//    public
//    @ResponseBody
//    Map getOggTotal(HttpServletRequest request, HttpServletResponse response) {
//        logger.info("com.winit.ogg.web.OggController.getOggTotal");
//        return operateService.OGGDataCount();
//    }

    @RequestMapping(value = "/getLog", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取同步日志")
    public
    @ResponseBody
    Map getLog(HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.getLog");
        return operateService.getLog();
    }

    @RequestMapping(value = "/cleanRedis", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "清空本地redis缓存")
    public
    @ResponseBody
    Map cleanRedis(HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.cleanRedis");
        return oggManager.cleanRedis();
    }

    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "校验")
    public
    @ResponseBody
    Map check(@ApiParam(value = "检查类型,不传默认为全局检查  all/white", required = false) @RequestParam(value = "type", required = false) String type,
              HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.check type=" + type);
        if (StringUtils.isEmpty(type)) {
            type = "all";
        }
        return operateService.check(type);
    }

    @RequestMapping(value = "/changelog", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询pms修改记录")
    public
    @ResponseBody
    Map getPmsChangeLog(@ApiParam(value = "查询时间,yyyy-MM-dd HH:mm:ss", required = false) @RequestParam(value = "date", required = false) String date,
                        HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.getPmsChangeLog date=" + date);
        if (StringUtils.isEmpty(date)) {
            Authorize authorize = JSONFileUtil.getFileJsonForBean(Constants.AUTHORIZE_FILE_PATH, Authorize.class);
            date = authorize.getCompleteDate();
        }
        Map map = new HashMap();
        map.put("code", "0");
        map.put("list", oggManager.getPmsChangeLog(date));
        return map;
    }

    @RequestMapping(value = "/complete", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "ogg同步完成操作")
    public
    @ResponseBody
    void OggComplete(HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.OggComplete");
        oggManager.afterOggComplete();
    }

    @RequestMapping(value = "/updateTom", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "更新tom配置文件")
    public
    @ResponseBody
    void UpdateTomConfig(@ApiParam(value = "状态", required = true) @RequestParam(value = "flag", required = true) Boolean flag,
                         HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.UpdateTomConfig flag=" + flag);
        oggManager.updateTomConfig(flag);
    }

    @RequestMapping(value = "/budget", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "预算配置同步")
    public
    @ResponseBody
    void Budget(HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.ogg.web.OggController.Budget");
        oggManager.BudgetConfig();
    }

    @RequestMapping(value = "/noPower", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    Map noPower(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", "1");
        rtnMap.put("desc", "当前ip没有访问权限");
        return rtnMap;
    }
}

